package com.caimore.common;

import com.caimore.common.SmsUtils_sdk;
import com.caimore.entity.SMSMessageMgr;
import com.caimore.entity.SMSRecvMsg;
import com.caimore.logger.Logger;
import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

public class Serial_For_Smslib {

   private SerialPort serialPort;
   public static Serial_For_Smslib SerialSmslib;
   ReentrantLock ReaderLock = new ReentrantLock();
   ReentrantLock WriterLock = new ReentrantLock();
   int PortId;
   int Baudrate;
   int TimeOut = 10;
   int FunCode = 3;
   int DataLen = 4;
   int AppendMillsec = 38;
   int Bytes = 90;
   int FrameInterval;


   public static Serial_For_Smslib getInstance() {
      if(SerialSmslib == null) {
         SerialSmslib = new Serial_For_Smslib();
      }

      return SerialSmslib;
   }

   public boolean openPort(final String sPortStr, int iBaudrate) {
      this.FrameInterval = getFrameInterval(this.AppendMillsec, this.Bytes, iBaudrate);
      if(this.serialPort == null) {
         this.serialPort = SerialPort.getCommPort(sPortStr);
      }

      this.serialPort.addDataListener(new SerialPortDataListener() {
         public int getListeningEvents() {
            return 1;
         }
         public void serialEvent(SerialPortEvent serialPortEvent) {
            if(serialPortEvent.getEventType() == 1) {
               try {
                  Serial_For_Smslib.this.ReaderLock.lock();
                  byte[] e = new byte[Serial_For_Smslib.this.serialPort.bytesAvailable()];
                  int numRead = Serial_For_Smslib.this.serialPort.readBytes(e, e.length);
                  String strData = new String(e, StandardCharsets.UTF_8);
                  Serial_For_Smslib.this.readBytesCMGL(sPortStr, strData);
               } catch (Exception var8) {
                  var8.printStackTrace();
               } finally {
                  Serial_For_Smslib.this.ReaderLock.unlock();
               }

            }
         }
      });
      this.serialPort.setFlowControl(0);
      this.serialPort.setComPortParameters(iBaudrate, 8, 1, 0);
      this.serialPort.setComPortTimeouts(272, 1000, 1000);
      this.serialPort.openPort();
      return this.serialPort.isOpen();
   }

   public void writeByte(char[] ch) throws Exception {
      if(this.serialPort != null && this.serialPort.isOpen()) {
         try {
            this.WriterLock.lock();
            String str = new String(ch);
            byte[] data = str.getBytes(StandardCharsets.UTF_8);
            if(this.serialPort != null) {
               this.serialPort.writeBytes(data, data.length);
            }

            Thread.sleep((long)this.FrameInterval);
         } finally {
            this.WriterLock.unlock();
         }

      } else {
         System.out.println("发送串口数据失败，数据串口未启动");
      }
   }

   public void writeByte8332(byte[] data) throws Exception {
      if(this.serialPort != null && this.serialPort.isOpen()) {
         try {
            this.WriterLock.lock();
            if(this.serialPort != null) {
               this.serialPort.writeBytes(data, data.length);
            }

            Thread.sleep((long)this.FrameInterval);
         } finally {
            this.WriterLock.unlock();
         }

      } else {
         System.out.println("发送串口数据失败，数据串口未启动");
      }
   }

   public boolean readByte(String sPortStr, String strData) throws Exception {
      boolean Rsbool = false;
      if(this.serialPort != null) {
         if(strData != null && strData.trim().length() > 0) {
            System.out.println("  readByte()rsStr:" + strData);
            String RsStr;
            if(strData.indexOf("CSCA:") >= 0) {
               int beginIndex = strData.indexOf("CSCA:");
               int endIndex = strData.indexOf(",");
               if(beginIndex >= 0 && endIndex >= 0 && endIndex > beginIndex) {
                  String sCSCA = strData.substring(beginIndex + 10, endIndex - 1);
                  if(sCSCA.trim().length() == 8) {
                     sCSCA = strData.substring(beginIndex + 7, endIndex - 1);
                  }

                  SMSMessageMgr.getInstance().setCSCA(sCSCA);
                  System.out.println("sCSCA= " + SMSMessageMgr.getInstance().getCSCA());
                  RsStr = "";
               }
            } else if(strData.indexOf("CPIN: READY") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               Rsbool = true;
               SMSMessageMgr.getInstance().setREADY(true);
               RsStr = "";
               System.out.println("SMSMessageMgr.getInstance().getREADY()" + SMSMessageMgr.getInstance().getREADY());
            } else if(strData.indexOf("ERROR") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               Rsbool = true;
               SMSMessageMgr.getInstance().setERROR(2);
               RsStr = "";
            } else if(strData.indexOf("M35") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("M35");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MG323") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MC37i");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MC37iR3") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MC37i");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MC8332") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MC8332");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MC8630") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MC8630");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MU509") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MU509");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MU709") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MU709");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("U7500") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("U7500");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MU609") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MU609");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("MC8630") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("MC8630");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("GL868") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("GL868");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("GL865") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("GL865");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("SIM7600CE") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("SIM7600CE");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("N720") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("N720");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("EC20F") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("EC20F");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("EC200N") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("EC200N");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("EC200S") >= 0) {
               SMSMessageMgr.getInstance().setSmsType("EC200S");
               Rsbool = true;
               RsStr = "";
            } else if(strData.indexOf("OK") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               Rsbool = true;
               SMSMessageMgr.getInstance().setOK(true);
               RsStr = "";
            } else if(strData.indexOf(">") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               Rsbool = true;
               SMSMessageMgr.getInstance().setCR(true);
               RsStr = "";
            } else {
               RsStr = "";
            }
         } else {
            System.out.println(sPortStr + ":readByte");
         }
      }

      Thread.sleep((long)this.FrameInterval);
      return Rsbool;
   }

   public String readBytesCMGL(String sPortStr, String strData) throws Exception {
      String RsStr = strData;

      try {
         if(this.serialPort != null) {
            if(RsStr != null && RsStr.trim().length() > 0) {
               System.out.println(SmsUtils_sdk.getNowTimeString() + "  readBytesCMGL()rsStr:" + RsStr);

               ArrayList e;
               int endIndex;
               for(; RsStr.indexOf("CMGL:") >= 0; e = null) {
                  e = new ArrayList();
                  endIndex = RsStr.indexOf("CMGL:");
                  if(endIndex >= 0) {
                     RsStr = RsStr.substring(endIndex);
                     int sCSCA = RsStr.indexOf(",");
                     int endIndexCR;
                     String sCMGL;
                     if(sCSCA >= 0) {
                        String endIndex1 = RsStr.substring(5, sCSCA);
                        endIndexCR = Integer.parseInt(endIndex1.trim());
                        System.out.println("ismsi=" + endIndexCR);
                        sCMGL = "AT+CMGD=" + endIndexCR + "\r";
                        char[] index050003 = sCMGL.toCharArray();
                        this.writeByte(index050003);
                        System.out.println("[readBytesCMGL]sSend-AT-Command:" + sCMGL);
                     }

                     int var26 = RsStr.indexOf("08");
                     if(var26 >= 0) {
                        RsStr = RsStr.substring(var26, RsStr.length());
                        endIndexCR = RsStr.indexOf("\r\n");
                        System.out.println("[readBytesCMGL]endIndexCR:" + endIndexCR);

                        while(endIndexCR >= 0) {
                           sCMGL = RsStr.substring(0, endIndexCR);
                           int var27 = sCMGL.indexOf("050003");
                           SMSMessageMgr.getInstance().setCMGL(sCMGL);
                           String sReceviedsPhoneNo = SmsUtils_sdk.getReceivedPhoneNo(sCMGL);
                           String sReceivedTime = SmsUtils_sdk.getReceivedTime(sCMGL);
                           System.out.println("[readBytesCMGL]index050003=" + var27);
                           String sSMSMessageContent;
                           if(var27 > 0) {
                              sSMSMessageContent = "other";
                              sSMSMessageContent = SmsUtils_sdk.getEC200N7bitorUCS2(sCMGL);
                              String oSMSMessage = sCMGL.substring(var27 + 10, var27 + 12);
                              System.out.println("[readBytesCMGL]sSeriNo:" + oSMSMessage);
                              String sAllNo = sCMGL.substring(var27 + 8, var27 + 10);
                              System.out.println("[readBytesCMGL]sAllNo:" + sAllNo);
                              String sCMGL2 = sCMGL.substring(var27 + 12);
                              System.out.println("[readBytesCMGL]sCMGL2:" + sCMGL2);
                              boolean iSeriNo = false;
                              boolean iAllNo = false;
                              int var29 = Integer.parseInt(oSMSMessage);
                              int var30 = Integer.parseInt(sAllNo);
                              System.out.println("[readBytesCMGL]iSeriNo:" + var29);
                              System.out.println("[readBytesCMGL]iAllNo:" + var30);
                              if(var29 > 0 && var30 > 0) {
                                 String isize;
                                 if("7bit".equals(sSMSMessageContent)) {
                                    isize = SmsUtils_sdk.decode7bit(sCMGL2);
                                    System.out.println("decode=" + isize);
                                    SMSMessageMgr.getInstance().longSMSMessage.put(Integer.valueOf(var29), isize);
                                 } else if("UCS2".equals(sSMSMessageContent)) {
                                    isize = SmsUtils_sdk.decodeUCS2(sCMGL2);
                                    System.out.println("decode=" + isize);
                                    SMSMessageMgr.getInstance().longSMSMessage.put(Integer.valueOf(var29), isize);
                                 } else {
                                    SMSMessageMgr.getInstance().longSMSMessage.put(Integer.valueOf(var29), sCMGL2);
                                 }

                                 int var31 = SMSMessageMgr.getInstance().longSMSMessage.size();
                                 System.out.println("[readBytesCMGL]isize:" + var31);
                                 if(var29 == var30 && var29 > 0) {
                                    StringBuffer sb = new StringBuffer();

                                    for(int sSMSMessageContent1 = 1; sSMSMessageContent1 <= var30; ++sSMSMessageContent1) {
                                       sb.append((String)SMSMessageMgr.getInstance().longSMSMessage.remove(Integer.valueOf(sSMSMessageContent1)));
                                    }

                                    String var32 = sb.toString();
                                    System.out.println("sSMSMessageContent= " + var32);
                                    SMSRecvMsg oSMSMessage1 = new SMSRecvMsg();
                                    oSMSMessage1.Phone = sReceviedsPhoneNo;
                                    oSMSMessage1.RecvTime = sReceivedTime;
                                    oSMSMessage1.Msg = var32;
                                    e.add(oSMSMessage1);
                                 }
                              }
                           } else {
                              sSMSMessageContent = SmsUtils_sdk.getSMSMessageContentEC200N(sCMGL);
                              System.out.println("sSMSMessageContent= " + sSMSMessageContent);
                              SMSRecvMsg var28 = new SMSRecvMsg();
                              var28.Phone = sReceviedsPhoneNo;
                              var28.RecvTime = sReceivedTime;
                              var28.Msg = sSMSMessageContent;
                              e.add(var28);
                           }

                           RsStr = RsStr.substring(endIndexCR, RsStr.length());
                           endIndex = RsStr.indexOf("CMGL:");
                           if(endIndex < 0) {
                              break;
                           }

                           RsStr = RsStr.substring(endIndex);
                           var26 = RsStr.indexOf("08");
                           if(var26 < 0) {
                              break;
                           }

                           RsStr = RsStr.substring(var26, RsStr.length());
                           endIndexCR = RsStr.indexOf("\r\n");
                        }
                     }
                  }

                  if(e.size() > 0) {
                     SMSMessageMgr.getInstance().addRecCMGLMessage(e);
                  }
               }

               int var24;
               while(RsStr.indexOf("CMGS:") >= 0) {
                  var24 = RsStr.indexOf("CMGS:");
                  RsStr = RsStr.substring(var24 + 5, RsStr.length());
                  Logger.info("[readBytesCMGL]SendSMStime: " + SmsUtils_sdk.getNowTimeString());
               }

               while(RsStr.indexOf("CSCA:") >= 0) {
                  var24 = RsStr.indexOf("CSCA:");
                  endIndex = RsStr.indexOf(",");
                  if(var24 >= 0 && endIndex >= 0 && endIndex > var24) {
                     String var25 = RsStr.substring(var24 + 10, endIndex - 1);
                     if(var25.trim().length() == 8) {
                        var25 = RsStr.substring(var24 + 7, endIndex - 1);
                     }

                     SMSMessageMgr.getInstance().setCSCA(var25);
                     System.out.println("sCSCA= " + SMSMessageMgr.getInstance().getCSCA());
                     RsStr = "";
                  }
               }

               while(RsStr.indexOf("CPIN: READY") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setREADY(true);
                  RsStr = "";
                  System.out.println("SMSMessageMgr.getInstance().getREADY() " + SMSMessageMgr.getInstance().getREADY());
               }

               while(RsStr.indexOf("CPIN:READY") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setREADY(true);
                  RsStr = "";
                  System.out.println("SMSMessageMgr.getInstance().getREADY() " + SMSMessageMgr.getInstance().getREADY());
               }

               while(RsStr.indexOf("ERROR") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setERROR(2);
                  RsStr = "";
               }

               while(RsStr.indexOf("M35") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("M35");
                  RsStr = "";
               }

               while(RsStr.indexOf("MG323") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MC37i");
                  RsStr = "";
               }

               while(RsStr.indexOf("MC37iR3") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MC37i");
                  RsStr = "";
               }

               while(RsStr.indexOf("MC8332") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MC8332");
                  RsStr = "";
               }

               while(RsStr.indexOf("MC8630") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MC8630");
                  RsStr = "";
               }

               while(RsStr.indexOf("MU509") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MU509");
                  RsStr = "";
               }

               while(RsStr.indexOf("MU709") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MU709");
                  RsStr = "";
               }

               while(RsStr.indexOf("U7500") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("U7500");
                  RsStr = "";
               }

               while(RsStr.indexOf("MU609") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MU609");
                  RsStr = "";
               }

               while(RsStr.indexOf("MC8630") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("MC8630");
                  RsStr = "";
               }

               while(RsStr.indexOf("GL868") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("GL868");
                  RsStr = "";
               }

               while(RsStr.indexOf("GL865") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("GL865");
                  RsStr = "";
               }

               while(RsStr.indexOf("SIM7600CE") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("SIM7600CE");
                  RsStr = "";
               }

               while(RsStr.indexOf("N720") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("N720");
                  RsStr = "";
               }

               while(RsStr.indexOf("EC20F") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("EC20F");
                  RsStr = "";
               }

               while(RsStr.indexOf("EC200N") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("EC200N");
                  RsStr = "";
               }

               while(RsStr.indexOf("EC200S") >= 0) {
                  SMSMessageMgr.getInstance().setSmsType("EC200S");
                  RsStr = "";
               }

               while(RsStr.indexOf("OK") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setOK(true);
                  RsStr = "";
               }

               while(RsStr.indexOf(">") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setCR(true);
                  RsStr = "";
               }
            } else {
               System.out.println(sPortStr + " :readBytesCMGL");
            }
         }

         Thread.sleep((long)this.FrameInterval);
      } catch (Exception var23) {
         var23.printStackTrace();
      }

      return RsStr;
   }

   public String readBytesMU509(String sPortStr, String strData) throws Exception {
      String RsStr = strData;
      if(this.serialPort != null) {
         if(strData != null && strData.trim().length() > 0) {
            System.out.println("  readBytesMU509()rsStr:" + strData);

            ArrayList beginIndex;
            int endIndex;
            for(; RsStr.indexOf("CMGL:") >= 0; beginIndex = null) {
               beginIndex = new ArrayList();
               endIndex = RsStr.indexOf("CMGL:");
               int sCSCA = RsStr.indexOf("08");
               if(endIndex >= 0 && sCSCA >= 0 && sCSCA > endIndex) {
                  RsStr = RsStr.substring(sCSCA, RsStr.length());
                  int endIndexCR = RsStr.indexOf("\r\n");
                  if(endIndexCR >= 0) {
                     String sCMGL = RsStr.substring(0, endIndexCR);
                     SMSMessageMgr.getInstance().setCMGL(sCMGL);
                     System.out.println("[readBytesMU509]sCMGL= " + SMSMessageMgr.getInstance().getCMGL());
                     String sReceviedsPhoneNo = SmsUtils_sdk.getReceivedPhoneNoMU509(sCMGL);
                     System.out.println("sReceviedPhoneMU509= " + sReceviedsPhoneNo);
                     String sReceivedTime = SmsUtils_sdk.getReceivedTimeMU509(sCMGL);
                     System.out.println("sReceivedTimeMU509= " + sReceivedTime);
                     String sSMSMessageContent = SmsUtils_sdk.getSMSMessageContentMU509(sCMGL);
                     System.out.println("sSMSMessageContentMU509= " + sSMSMessageContent);
                     SMSRecvMsg oSMSMessage = new SMSRecvMsg();
                     oSMSMessage.Phone = sReceviedsPhoneNo;
                     oSMSMessage.RecvTime = sReceivedTime;
                     oSMSMessage.Msg = sSMSMessageContent;
                     beginIndex.add(oSMSMessage);
                     RsStr = RsStr.substring(endIndexCR, RsStr.length());
                  }
               }

               if(beginIndex.size() > 0) {
                  SMSMessageMgr.getInstance().addRecCMGLMessage(beginIndex);
               }
            }

            int beginIndex1;
            while(RsStr.indexOf("CMGS:") >= 0) {
               beginIndex1 = RsStr.indexOf("CMGS:");
               RsStr = RsStr.substring(beginIndex1 + 5, RsStr.length());
               Logger.info("[readBytesMU509]SendSMStime: " + SmsUtils_sdk.getNowTimeString());
            }

            while(RsStr.indexOf("CSCA:") >= 0) {
               beginIndex1 = RsStr.indexOf("CSCA:");
               endIndex = RsStr.indexOf(",");
               if(beginIndex1 >= 0 && endIndex >= 0 && endIndex > beginIndex1) {
                  String sCSCA1 = RsStr.substring(beginIndex1 + 10, endIndex - 1);
                  if(sCSCA1.trim().length() == 8) {
                     sCSCA1 = RsStr.substring(beginIndex1 + 7, endIndex - 1);
                  }

                  SMSMessageMgr.getInstance().setCSCA(sCSCA1);
                  System.out.println("sCSCA= " + SMSMessageMgr.getInstance().getCSCA());
                  RsStr = "";
               }
            }

            while(RsStr.indexOf("CPIN: READY") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               SMSMessageMgr.getInstance().setREADY(true);
               RsStr = "";
               System.out.println("SMSMessageMgr.getInstance().getREADY() " + SMSMessageMgr.getInstance().getREADY());
            }

            while(RsStr.indexOf("CPIN:READY") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               SMSMessageMgr.getInstance().setREADY(true);
               RsStr = "";
               System.out.println("SMSMessageMgr.getInstance().getREADY() " + SMSMessageMgr.getInstance().getREADY());
            }

            while(RsStr.indexOf("ERROR") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               SMSMessageMgr.getInstance().setERROR(2);
               RsStr = "";
            }

            while(RsStr.indexOf("OK") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               SMSMessageMgr.getInstance().setOK(true);
               RsStr = "";
            }

            while(RsStr.indexOf(">") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               SMSMessageMgr.getInstance().setCR(true);
               RsStr = "";
            }
         } else {
            System.out.println(sPortStr + " :readBytesMU509");
         }
      }

      Thread.sleep((long)this.FrameInterval);
      return RsStr;
   }

   public String readBytes8332PDU(String sPortStr, String strData) throws Exception {
      String rsStr = strData;
      String sMT = null;
      if(this.serialPort != null) {
         if(strData != null && strData.trim().length() > 0) {
            System.out.println("  readBytes8332PDU()rsStr:" + strData);

            ArrayList lstSMSMessage;
            int beginIndex;
            int endIndex;
            for(; rsStr.indexOf("CMTI") > 0; SMSMessageMgr.getInstance().addCMGL8332(lstSMSMessage)) {
               lstSMSMessage = new ArrayList();
               beginIndex = rsStr.indexOf("\"MT\",");
               if(beginIndex >= 0) {
                  rsStr = rsStr.substring(beginIndex, rsStr.length());
                  endIndex = rsStr.indexOf("\r\n");
                  if(endIndex >= 0) {
                     sMT = rsStr.substring(5, endIndex);
                     lstSMSMessage.add(sMT);
                  }
               }
            }

            for(; rsStr.indexOf("CMGR:") > 0; lstSMSMessage = null) {
               lstSMSMessage = new ArrayList();
               beginIndex = rsStr.indexOf("CMGR:");
               if(beginIndex >= 0) {
                  rsStr = rsStr.substring(beginIndex, rsStr.length());
                  endIndex = rsStr.indexOf("0B");
                  if(endIndex >= 0) {
                     rsStr = rsStr.substring(endIndex, rsStr.length());
                     int endIndexCR = rsStr.indexOf("\r\n");
                     if(endIndexCR >= 0) {
                        String sCMGR = rsStr.substring(0, endIndexCR);
                        SMSMessageMgr.getInstance().setCMGL(sCMGR);
                        System.out.println("[readBytes8332PDU]sCMGL= " + SMSMessageMgr.getInstance().getCMGL());
                        String sReceviedsPhoneNo = SmsUtils_sdk.getReceivedPhoneNo8332(sCMGR);
                        System.out.println("sReceviedPhone= " + sReceviedsPhoneNo);
                        String sReceivedTime = SmsUtils_sdk.getReceivedTime8332(sCMGR);
                        System.out.println("sReceivedTime= " + sReceivedTime);
                        String sSMSMessageContent = SmsUtils_sdk.getSMSMessageContent8332(sCMGR);
                        System.out.println("sSMSMessageContent= " + sSMSMessageContent);
                        SMSRecvMsg oSMSMessage = new SMSRecvMsg();
                        oSMSMessage.Phone = sReceviedsPhoneNo;
                        oSMSMessage.RecvTime = sReceivedTime;
                        oSMSMessage.Msg = sSMSMessageContent;
                        lstSMSMessage.add(oSMSMessage);
                        rsStr = rsStr.substring(endIndexCR, rsStr.length());
                     }
                  }
               }

               if(lstSMSMessage.size() > 0) {
                  SMSMessageMgr.getInstance().addRecCMGLMessage(lstSMSMessage);
               }
            }

            while(rsStr.indexOf("ERROR") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               SMSMessageMgr.getInstance().setERROR(2);
               rsStr = "";
            }

            while(rsStr.indexOf("OK") >= 0) {
               System.out.println("找到" + sPortStr + ":短信模块串口");
               SMSMessageMgr.getInstance().setOK(true);
               rsStr = "";
            }
         } else {
            System.out.println(sPortStr + " :readBytes8332PDU");
         }
      }

      Thread.sleep((long)this.FrameInterval);
      return sMT;
   }

   public String readBytes8332TEXT(String sPortStr, byte[] byData) throws Exception {
      String rsStr = "";
      String sMT = null;

      try {
         if(this.serialPort != null) {
            if(byData != null) {
               rsStr = SmsUtils_sdk.bytesToHexString(byData);
               System.out.println("  readBytes8332TEXT()  rsStr:" + rsStr);
               String e = SmsUtils_sdk.hexString2intString(rsStr);
               System.out.println("  readBytes8332TEXT()  SmsUtils.hexString2intString(rsStr): " + e);
               ArrayList lstMT = new ArrayList();

               int iBegin;
               while(rsStr.indexOf("434D5449") >= 0) {
                  int lstCMGL = rsStr.indexOf("224D54222C");
                  if(lstCMGL >= 0) {
                     rsStr = rsStr.substring(lstCMGL, rsStr.length());
                     iBegin = rsStr.indexOf("0D0A");
                     if(iBegin >= 0) {
                        sMT = rsStr.substring(10, iBegin);
                        sMT = SmsUtils_sdk.hexString2intString(sMT);
                        lstMT.add(sMT);
                     }

                     SMSMessageMgr.getInstance().addCMGL8332(lstMT);
                  }
               }

               lstMT = null;

               int endIndexCR;
               String sHCMGS;
               ArrayList lstCMGL1;
               for(lstCMGL1 = new ArrayList(); rsStr.indexOf("434D474C3A") >= 0; SMSMessageMgr.getInstance().addCMGL8332(lstCMGL1)) {
                  iBegin = rsStr.indexOf("434D474C3A");
                  if(iBegin < 0) {
                     break;
                  }

                  rsStr = rsStr.substring(iBegin + 10, rsStr.length());
                  endIndexCR = rsStr.indexOf("2C");
                  if(endIndexCR >= 0) {
                     sHCMGS = rsStr.substring(0, endIndexCR);
                     sHCMGS = SmsUtils_sdk.hexString2intString(sHCMGS);
                     lstCMGL1.add(sHCMGS);
                  }
               }

               int iIndex2C3;
               int iIndex2C4;
               int iIndex2C5;
               ArrayList endIndexCR1;
               int sHCMGS1;
               for(lstCMGL1 = null; rsStr.indexOf("434D47523A") >= 0; endIndexCR1 = null) {
                  iBegin = rsStr.indexOf("434D47523A");
                  if(iBegin < 0) {
                     break;
                  }

                  endIndexCR1 = new ArrayList();
                  rsStr = rsStr.substring(iBegin + 10, rsStr.length());
                  sHCMGS1 = rsStr.indexOf("2C");
                  if(sHCMGS1 >= 0) {
                     iIndex2C3 = rsStr.indexOf("2C", sHCMGS1 + 1);
                     iIndex2C4 = rsStr.indexOf("2C", iIndex2C3 + 1);
                     iIndex2C5 = rsStr.indexOf("0D0A");
                     int sSendTime = rsStr.indexOf("0D0A", iIndex2C5 + 1);
                     String sReceviedsPhoneNo = "";
                     String sReceivedTime = "";
                     String sSMSMessageContentus2 = "";
                     if(iIndex2C3 >= 0) {
                        sReceviedsPhoneNo = rsStr.substring(sHCMGS1 + 4, iIndex2C3 - 1);
                        sReceviedsPhoneNo = SmsUtils_sdk.hexString2intString(sReceviedsPhoneNo);
                        if(iIndex2C4 >= 0) {
                           sReceivedTime = rsStr.substring(iIndex2C3 + 4, iIndex2C4 - 1);
                           sReceivedTime = SmsUtils_sdk.hexString2intString(sReceivedTime);
                           sReceivedTime = sReceivedTime.replace("/", "-");
                        }
                     }

                     if(iIndex2C5 >= 0 && sSendTime >= 0 && sSendTime > iIndex2C5) {
                        String oSMSMessage = rsStr.substring(iIndex2C5 + 4, sSendTime);
                        sSMSMessageContentus2 = SmsUtils_sdk.decode8332UCS2(oSMSMessage);
                        System.out.println("sSMSMessageContentus2: " + sSMSMessageContentus2);
                     }

                     if(sReceviedsPhoneNo.trim().length() > 0 && sReceivedTime.trim().length() > 0 && sSMSMessageContentus2.trim().length() > 0) {
                        SMSRecvMsg oSMSMessage1 = new SMSRecvMsg();
                        oSMSMessage1.Phone = sReceviedsPhoneNo;
                        oSMSMessage1.RecvTime = sReceivedTime;
                        oSMSMessage1.Msg = sSMSMessageContentus2;
                        endIndexCR1.add(oSMSMessage1);
                     }
                  }

                  if(endIndexCR1.size() > 0) {
                     SMSMessageMgr.getInstance().addRecCMGLMessage(endIndexCR1);
                  }
               }

               while(rsStr.indexOf("4344533A") >= 0) {
                  iBegin = rsStr.indexOf("4344533A");
                  if(iBegin < 0) {
                     break;
                  }

                  rsStr = rsStr.substring(iBegin + 8, rsStr.length());
                  endIndexCR = rsStr.indexOf("2C");
                  if(endIndexCR >= 0) {
                     sHCMGS1 = rsStr.indexOf("2C", endIndexCR + 1);
                     iIndex2C3 = rsStr.indexOf("2C", sHCMGS1 + 1);
                     iIndex2C4 = rsStr.indexOf("2C", iIndex2C3 + 1);
                     iIndex2C5 = rsStr.indexOf("2C", iIndex2C4 + 1);
                     String sSendTime1 = "";
                     if(sHCMGS1 >= 0 && iIndex2C3 >= 0 && iIndex2C3 > sHCMGS1 && iIndex2C4 >= 0 && iIndex2C5 >= 0 && iIndex2C5 > iIndex2C4) {
                        if(iIndex2C5 - 2 >= iIndex2C4 + 4) {
                           sSendTime1 = rsStr.substring(iIndex2C4 + 4, iIndex2C5 - 2);
                           sSendTime1 = SmsUtils_sdk.hexString2intString(sSendTime1);
                           sSendTime1 = sSendTime1.replace("/", "-");
                        }

                        if(sSendTime1.trim().length() > 0) {
                           Logger.info("[readBytes8332TEXT]SendSMStime: " + sSendTime1);
                        }
                     }
                  }
               }

               while(e.indexOf("CPIN: READY") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setREADY(true);
                  rsStr = "";
                  e = "";
                  System.out.println("  readBytes8332TEXT()  SMSMessageMgr.getInstance().getREADY()" + SMSMessageMgr.getInstance().getREADY());
               }

               while(e.indexOf("CPIN:READY") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setREADY(true);
                  rsStr = "";
                  e = "";
                  System.out.println("  readBytes8332TEXT()  SMSMessageMgr.getInstance().getREADY()" + SMSMessageMgr.getInstance().getREADY());
               }

               while(e.indexOf("^HCMGSS:") >= 0) {
                  Logger.info("[readBytes8332TEXT]HCMGSS SendSMStime: " + SmsUtils_sdk.getNowTimeString());
                  rsStr = "";
                  e = "";
               }

               while(e.indexOf("^HCMGS:") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  iBegin = e.indexOf("^HCMGS:");
                  endIndexCR = e.indexOf("\r\n", iBegin + 1);
                  if(endIndexCR > 0 && endIndexCR > iBegin) {
                     sHCMGS = e.substring(iBegin + 7, endIndexCR);
                     SMSMessageMgr.getInstance().setHCMGS(sHCMGS);
                  }

                  rsStr = "";
                  e = "";
                  System.out.println("  readBytes8332TEXT()  SMSMessageMgr.getInstance().getHCMGS()" + SMSMessageMgr.getInstance().getHCMGS());
               }

               while(e.indexOf("ERROR") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setERROR(2);
                  rsStr = "";
                  e = "";
               }

               while(e.indexOf("OK") >= 0) {
                  System.out.println("找到" + sPortStr + ":短信模块串口");
                  SMSMessageMgr.getInstance().setOK(true);
                  rsStr = "";
                  e = "";
               }
            } else {
               System.out.println(sPortStr + ":readBytes8332TEXT");
            }
         }

         Thread.sleep((long)this.FrameInterval);
      } catch (Exception var19) {
         System.out.println("[Serial_For_Smslib . readBytes8332TEXT]" + var19.getMessage());
      }

      return sMT;
   }

   public void closePort() {
      if(this.serialPort != null) {
         this.serialPort.closePort();
         this.serialPort = null;
      }

   }

   public static int getFrameInterval(int iAppendMillsec, int iDataLen, int iBaudrate) {
      int rsInt = (int)Math.ceil((double)((float)(12 * (iDataLen + 4) * 1000) / (float)iBaudrate)) + iAppendMillsec;
      return rsInt;
   }

   public static final String bytesToHexString(char[] chs) {
      StringBuffer sb = new StringBuffer(chs.length);

      for(int i = 0; i < chs.length; ++i) {
         String sTemp = Integer.toHexString(255 & chs[i]);
         if(sTemp.length() < 2) {
            sb.append(0);
         }

         sb.append(sTemp.toUpperCase() + " ");
      }

      return sb.toString();
   }
}
